package api;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.Problem;
import dao.ProblemDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/problem")
public class ProblemServlet extends HttpServlet {
    // 这个 ObjectMapper 是 Jackson 中的一个重要的类
    private final ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setStatus(200);
        resp.setContentType("application/json;charset=utf8");

        // 获取全部的题目列表
        ProblemDao problemDao = new ProblemDao();
        List<Problem> problems = problemDao.selectAll();

        // 转化成json格式字符串
        String respString = objectMapper.writeValueAsString(problems);
        resp.getWriter().write(respString);
    }
}
